package pl.bank.model;

import java.util.Date;

import oracle.adf.share.logging.ADFLogger;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.Timestamp;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;

import pl.bank.model.common.BankAppModule;
import pl.bank.model.view.AccountsViewImpl;
import pl.bank.model.view.ClientsViewImpl;
import pl.bank.model.view.ClientsViewRowImpl;
import pl.bank.model.view.TransactionsViewImpl;


// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Thu Apr 19 20:07:43 CEST 2012
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class BankAppModuleImpl extends ApplicationModuleImpl implements BankAppModule {

    private static final ADFLogger LOG = ADFLogger.createADFLogger("BankAppModuleImpl");

    /**
     * This is the default constructor (do not remove).
     */
    public BankAppModuleImpl() {
    }


    public Row findRecipientsRow(Number key) {
        Object[] keyObj = new Object[1];
        keyObj[0] = key;
        Key keyDef = new Key(keyObj);
        Row[] rows = getDefinedrecipientsViewDef().findByKey(keyDef, 1);
        if (rows.length > 0) {
            return rows[0];
        } else {
            LOG.warning("Row is null");
            return null;
        }
    }


    public void setHistoryDates(Date startDate, Date endDate) {
        TransactionsViewImpl transactionsViewDef = (TransactionsViewImpl)getTransactionsViewDef();
        ViewCriteria criteria = transactionsViewDef.getViewCriteria("TransactionsViewCriteria");
        if (startDate != null) {
            transactionsViewDef.setStartDate(new Timestamp(startDate));
        }
        if (endDate != null) {
            transactionsViewDef.setEdnDate(new Timestamp(endDate));
        }
        criteria.getViewCriteriaManager().applyViewCriteria(criteria);
        LOG.info("Query : " + transactionsViewDef.getQuery());
        transactionsViewDef.executeQuery();
    }


    public Boolean validateLoginAndPassword(String login, String password) {
        LOG.info("login " + login + " password " + password);
        Boolean result = false;

        if (login == null || password == null) {
            LOG.warning("Password or login is null");
            return result;
        }
        ClientsViewImpl clientsViewDef = (ClientsViewImpl)getClientsViewDef();
        ViewCriteria criteria = clientsViewDef.getViewCriteria("ClientsViewCriteria");
        clientsViewDef.setLogin(login);
        criteria.getViewCriteriaManager().applyViewCriteria(criteria);

        LOG.info("Query : " + clientsViewDef.getQuery());

        clientsViewDef.executeQuery();

        Row[] allClients = clientsViewDef.getAllRowsInRange();
        if (clientsViewDef.getRowCountInRange() != 1) {
            LOG.info("Error not matching user");
            return result;
        }
        ClientsViewRowImpl cRow = (ClientsViewRowImpl)allClients[0];
        String passwordFromDatabase = cRow.getClPassword();

        if (passwordFromDatabase == null) {
            LOG.info("Password in database is null");
            return result;
        }

        if (passwordFromDatabase.toUpperCase().equalsIgnoreCase(password.toUpperCase())) {
            LOG.fine("Password is correct");
            clientsViewDef.setCurrentRow(cRow);
            setCurrentAccount(cRow.getClClientId().getSequenceNumber());

            result = true;
        }
        return result;

    }


    /**
     * Container's getter for ClientsViewDef.
     * @return ClientsViewDef
     */
    public ViewObjectImpl getClientsViewDef() {
        return (ViewObjectImpl)findViewObject("ClientsViewDef");
    }

    /**
     * Container's getter for ClientsAccountViewDef.
     * @return ClientsAccountViewDef
     */
    public ViewObjectImpl getClientsAccountViewDef() {
        return (ViewObjectImpl)findViewObject("ClientsAccountViewDef");
    }

    /**
     * Container's getter for AccountclientsClientsFkLink1.
     * @return AccountclientsClientsFkLink1
     */
    public ViewLinkImpl getAccountclientsClientsFkLink1() {
        return (ViewLinkImpl)findViewLink("AccountclientsClientsFkLink1");
    }

    /**
     * Container's getter for AccountsViewDef.
     * @return AccountsViewDef
     */
    public ViewObjectImpl getAccountsViewDef() {
        return (ViewObjectImpl)findViewObject("AccountsViewDef");
    }

    /**
     * Container's getter for AccountclientsViewDef.
     * @return AccountclientsViewDef
     */
    public ViewObjectImpl getAccountclientsViewDef() {
        return (ViewObjectImpl)findViewObject("AccountclientsViewDef");
    }

    /**
     * Container's getter for AccountclientsAccountsFkLink1.
     * @return AccountclientsAccountsFkLink1
     */
    public ViewLinkImpl getAccountclientsAccountsFkLink1() {
        return (ViewLinkImpl)findViewLink("AccountclientsAccountsFkLink1");
    }

    /**
     * Container's getter for AccountmessagesViewDef.
     * @return AccountmessagesViewDef
     */
    public ViewObjectImpl getAccountmessagesViewDef() {
        return (ViewObjectImpl)findViewObject("AccountmessagesViewDef");
    }

    /**
     * Container's getter for AccountmessagesAccountsFkLink1.
     * @return AccountmessagesAccountsFkLink1
     */
    public ViewLinkImpl getAccountmessagesAccountsFkLink1() {
        return (ViewLinkImpl)findViewLink("AccountmessagesAccountsFkLink1");
    }

    /**
     * Container's getter for DefinedrecipientsViewDef.
     * @return DefinedrecipientsViewDef
     */
    public ViewObjectImpl getDefinedrecipientsViewDef() {
        return (ViewObjectImpl)findViewObject("DefinedrecipientsViewDef");
    }

    /**
     * Container's getter for DefinedrecipientsAccountsFkLink1.
     * @return DefinedrecipientsAccountsFkLink1
     */
    public ViewLinkImpl getDefinedrecipientsAccountsFkLink1() {
        return (ViewLinkImpl)findViewLink("DefinedrecipientsAccountsFkLink1");
    }

    /**
     * Container's getter for StandingordersViewDef.
     * @return StandingordersViewDef
     */
    public ViewObjectImpl getStandingordersViewDef() {
        return (ViewObjectImpl)findViewObject("StandingordersViewDef");
    }

    /**
     * Container's getter for StandingordersAccountsFkLink1.
     * @return StandingordersAccountsFkLink1
     */
    public ViewLinkImpl getStandingordersAccountsFkLink1() {
        return (ViewLinkImpl)findViewLink("StandingordersAccountsFkLink1");
    }

    /**
     * Container's getter for TransactionsViewDef.
     * @return TransactionsViewDef
     */
    public ViewObjectImpl getTransactionsViewDef() {
        return (ViewObjectImpl)findViewObject("TransactionsViewDef");
    }

    /**
     * Container's getter for TransactionsAccountsFkLink1.
     * @return TransactionsAccountsFkLink1
     */
    public ViewLinkImpl getTransactionsAccountsFkLink1() {
        return (ViewLinkImpl)findViewLink("TransactionsAccountsFkLink1");
    }

    /**
     * Container's getter for MessagesViewDef.
     * @return MessagesViewDef
     */
    public ViewObjectImpl getMessagesViewDef() {
        return (ViewObjectImpl)findViewObject("MessagesViewDef");
    }

    /**
     * Container's getter for MessagesAccountViewDef.
     * @return MessagesAccountViewDef
     */
    public ViewObjectImpl getMessagesAccountViewDef() {
        return (ViewObjectImpl)findViewObject("MessagesAccountViewDef");
    }

    /**
     * Container's getter for AccountmessagesMessagesFkLink1.
     * @return AccountmessagesMessagesFkLink1
     */
    public ViewLinkImpl getAccountmessagesMessagesFkLink1() {
        return (ViewLinkImpl)findViewLink("AccountmessagesMessagesFkLink1");
    }

    private void setCurrentAccount(Number number) {
        AccountsViewImpl accountsViewDef = (AccountsViewImpl)getAccountsViewDef();
        ViewCriteria criteria = accountsViewDef.getViewCriteria("AccountsViewCriteria");
        accountsViewDef.setAccountNumber(number);
        criteria.getViewCriteriaManager().applyViewCriteria(criteria);
        accountsViewDef.executeQuery();
    }

    /**
     * Container's getter for MessagesForAccountViewDef.
     * @return MessagesForAccountViewDef
     */
    public ViewObjectImpl getMessagesForAccountViewDef() {
        return (ViewObjectImpl)findViewObject("MessagesForAccountViewDef");
    }

    /**
     * Container's getter for MessagesForAccountLink1.
     * @return MessagesForAccountLink1
     */
    public ViewLinkImpl getMessagesForAccountLink1() {
        return (ViewLinkImpl)findViewLink("MessagesForAccountLink1");
    }
}
